import os
import socket
import sys


def get_python_path():
    """
    获取python路径
    :return:
    """
    host_name = socket.gethostname()
    if host_name == "legend":
        python_path = "/home/legend/WorkSpace/python/water_applets/"
    if host_name == "ecs-364582":
        python_path = "/root/water_applets/"
    else:
        python_path = ""
    return python_path


sys.path.append(get_python_path())
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "water_applets.settings")
django.setup()
from models.models import Api
from apps.manager.advertising_manager.views import manager_index_router
from apps.manager.customer_service.views import manager_customer_service_router
from apps.manager.good_manager.views import good_manager_router
from apps.manager.order_info_manager.views import order_manager_router
from apps.manager.package.views import manage_package_router
from apps.manager.user_manager.api_views import api_router
from apps.manager.user_manager.manager_views import user_manager_router
from apps.manager.user_manager.menus import menu_router
from apps.manager.user_manager.permission_group_views import permission_group_router
from apps.manager.user_manager.user import manager_user_router
from apps.manager.water_card_record.views import manager_water_card_record_router
from apps.manager.water_card_transfer_record.views import manager_water_card_transfer_router
from apps.manager.water_droplets_task.views import water_droplets_router

method_map = {
    'get': 0,
    'post': 1,
    'delete': 2,
    'put': 3,
}

routers = [manager_index_router, manager_customer_service_router, good_manager_router, order_manager_router,
           manage_package_router, api_router, user_manager_router, menu_router, permission_group_router,
           manager_user_router, manager_water_card_record_router, manager_water_card_transfer_router,
           water_droplets_router]


def create_apis():
    Api.objects.all().delete()
    id = 1
    for router in routers:
        for path, value in router.path_operations.items():
            for op in value.operations:
                data = {
                    'id': id,
                    'position': f"/manager{path}",
                    'method': method_map[op.methods[0].lower()],
                    'name': op.summary,
                    'group': router.tags[0],
                    'user_default': op.openapi_extra.get('user_default', False) if op.openapi_extra else 0
                }
                Api.objects.create(**data)
                id += 1


if __name__ == '__main__':
    create_apis()
